#!/bin/sh
#
# run -- deploy a docker lab
#

lab_name=$1
lab_user=$2
lab_total=$3
# In hours
lab_life=$4

uname | grep -q MINGW && PWD_OPT="-W"
TOP_DIR="$(cd "$(dirname "$0")"/../../ && pwd $PWD_OPT)"
. "$TOP_DIR/tools/docker/config" $* >/dev/null

[ -z "$lab_identify" ] && lab_identify=1
[ -z "$lab_total" ] && lab_total=1
[ -z "$lab_user" ] && lab_user=ubuntu
[ -z "$lab_life" ] && lab_life=24H
[ -z "$lab_name" ] && lab_name=$CURRENT
[ -n "$lab_alive" ] && lab_life=$lab_alive

# Verify cpuset_cpus
[ -n "$lab_cpus" ] && verify_cpus $lab_cpus

[ ! -d "$LAB_OUTPUT" ] && mkdir -p "$LAB_OUTPUT"
VNC_IDENTIFY=1; [ $lab_identify -eq 0 ] && VNC_IDENTIFY=0

# Start the transparent proxy via tinylab/cloud-ubuntu-proxy_client_transparent container
(docker ps -f name=$TPROXY_NAME -f status=exited | grep -q -v PORTS) \
  && echo "LOG: Resume the stopped $TPROXY_NAME" \
  && docker start `docker ps -f name=$TPROXY_NAME -f status=exited -q`

(docker ps -f name=$TPROXY_NAME | grep -q -v PORTS)
  [ $? -ne 0 -a $TPROXY -eq 1 -a -n "$PROXY_SERVER" -a -n "$PROXY_PWD" -a -n "$ENCRYPT_CMD" ] \
  && echo "LOG: Start $TPROXY_NAME" \
  && PROXY_SERVER=$PROXY_SERVER PROXY_PWD=$PROXY_PWD ENCRYPT_CMD=$ENCRYPT_CMD PROXY_LIMIT=$PROXY_LIMIT $DOCKER_TPROXY_CMD $*

if [ $TPROXY -eq 1 ]; then
  TPROXY_IP=`docker inspect --format '{{ .NetworkSettings.IPAddress }}' $TPROXY_NAME`
  do_unlock
  set_var TPROXY_IP
  do_lock
fi

# Start the ssh/vnc proxy via tinylab/cloud-ubuntu-web container
(docker ps -f name=$WPROXY_NAME -f status=exited | grep -q -v PORTS) \
  && echo "LOG: Resume the stopped $WPROXY_NAME" \
  && docker start `docker ps -f name=$WPROXY_NAME -f status=exited -q`

(docker ps -f name=$WPROXY_NAME | grep -q -v PORTS) || \
  (echo "LOG: Start $WPROXY_NAME" \
  && "$DOCKER_EXPORT_CMD" $*)


OLD_TOP_DIR="$TOP_DIR"
OLD_LAB_OUTPUT="$LAB_OUTPUT"

tmpfile=`mktemp`

for i in `seq 1 $lab_total`
do
    LAB_SRC="$OLD_TOP_DIR"
    while :;
    do
       lab_name_random=`get_random`
       CONTAINER_NAME=${lab_name}-${lab_name_random}
       exist=`docker ps -q --filter=name=$CONTAINER_NAME | wc -l`
       [ $exist -eq 0 ] && break
    done

    LAB_PATH="${CONTAINER_NAME}+${lab_user}-${lab_life}-`date +"%Y%m%d-%H%M%S"`"
    LAB_DST="$OLD_LAB_OUTPUT/$LAB_PATH/"

    echo "LOG: Create $LAB_PATH"

    __lab_name=$lab_name
    [ "$lab_name" = "qing-lab" ] && __lab_name='*'
    [ ! -s "$tmpfile" ] && find "$LAB_SRC/configs/$lab_name" \
         "$LAB_SRC/labs/$__lab_name" \
         "$LAB_SRC/.git/modules/labs/$__lab_name" \
         "$LAB_SRC/tools" -type f -or -type d -or -type l | sed -e "s%$LAB_SRC%./%g" | grep -v "/docker/\." > $tmpfile

    if [ -z "$SUDO" ]; then
      rsync -ar -H "$LAB_SRC" "$LAB_DST/" --files-from=$tmpfile
    else
      $SUDO -u $HOST_USER rsync -ar -H "$LAB_SRC" "$LAB_DST/" --files-from=$tmpfile
    fi

    # Run it

    TOP_DIR="$LAB_DST"
    . "$TOP_DIR/tools/docker/config" $lab_name >/dev/null

    do_unlock

    set_var CURRENT
    set_var HOST
    set_var VNC_IDENTIFY

    if [ -n "$lab_mem" ]; then
       sed -i -e "/--memory/d" "$LAB_LIMITS"
       echo "--memory=$lab_mem" >> "$LAB_LIMITS"
    fi
    if [ -n "$lab_cpus" ]; then
      sed -i -e "/--cpuset-cpus/d" "$LAB_LIMITS"
      echo "--cpuset-cpus=$lab_cpus" >> "$LAB_LIMITS"
    fi

    do_lock

    UNIX_USER=$lab_user CONTAINER_NAME=$CONTAINER_NAME "$DOCKER_RUN_CMD" $lab_name
    [ -n "$lab_alive" ] && notify_user 0 "Note: Lab is only free for `time_to_strings $lab_life`." $lab_name
done

rm $tmpfile
